<?php
class Zendme_Models_CategoryService extends Zend_Db_Table
{
	protected $_name='service_category';
	protected $_primary='id';
	
	public function itemInSelectboxx($arrParam = null, $options = null){
		$db = $this->getAdapter();
		if($options == null){
			$select = $db->select()
						 ->from('service_category', array('id','name'));
			$result = $db->fetchPairs($select)	;
			$result[0] = ' -- Select an Item -- ';
			ksort($result);								 
		}
		return $result;
	}
	
	public function countItem($arrParam=null,$option=null)
	{
		
	}
	
	public function sortItem($arrParam = null, $options = null){			
		$items = $arrParam['items'];
		if(count($items)>0){
			foreach ($items as $key => $val) {
				$where = 'id = ' . $key;				
				$data = array('order'=>$val);
				$this->update($data,$where);
			}
		}
	}
	
	public function listItem($arrParam=null,$options=null)
	{
		$ssFilter = $arrParam['ssFilter'];
		$db	= $this->getAdapter();
		if($options['task'] == 'admin-list'){
			$select = $db	->select()
							->from("service_category AS c")
							->joinLeft("users AS u","u.id = c.created_by",array("u.user_name AS created_by"));							
			// Order..............
			$order = 'id DESC';
			$ssFilter = $arrParam['ssFilter'];
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$order = $ssFilter['col'] . ' ' . $ssFilter['order'];
			}
			$select->order($order);
						
			//Filter
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?',$keywords,STRING);
			}
			$result  = $db->fetchAll($select);
			$recursive = new Zendme_System_Recursive($result);
			$result = $recursive -> buildArray(0);
		}
		
		if($options['task']=='public')
		{
			$select = $db->select()
							->from("service_category AS c");
			$result=$db->fetchAll($select);					
		}		
		
		return $result;	
	}
	
	public function saveItem($arrParam=null,$options=null)
	{
		$info = new Zendme_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		if($options['task'] 	== 'admin-add'){
			$row 				= $this->fetchNew();
			$row->created		= date("Y-m-d-G-i-s");
			$row->created_by 	= $memberInfo;	
		}		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];			
			$row =  $this->fetchRow($where);
			$row->modified 		= date("Y-m-d-G-i-s");
			$row->modified_by 	= $memberInfo;
		}
		
		$mdlSlug=new Zendme_SlugFilter();
		$slug=$mdlSlug->createSlug($arrParam['name']);
		
		$row->slug=$slug;
		$row->name 		= $arrParam['name'];
		$row->parents 	= $arrParam['parents'];
		$row->summary	=$arrParam['summary'];
		if(!empty($arrParam['picture'])):$row->picture	= $arrParam['picture'];endif;
		$row->status 		= $arrParam['status'];
		$row->order 		= $arrParam['order'];	
		$row->home			=$arrParam['home'];
		$row->meta_description=$arrParam['meta_description'];
		$row->meta_keyword=$arrParam['meta_keyword'];
		$row->save();
		
	}
	
	public function getItem($arrParam=null,$options=null)
	{
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		if($options['task'] == 'admin-info'){
			$db = $this->getAdapter();
			$select = $db->select()
						 ->from('service_category AS c',array('id','name','status','order','parents','created_by','picture'))
						 ->joinLeft('service_category AS c2','c.parents = c2.id',array('name as parent_name'))
						 ->order('c.order ASC')
						 ->where('c.id =?',$arrParam['id']);
			$result = $db->fetchRow($select);
		}
		
		if($options['task']=='public-detail')
		{
			$select = $this->select()
						   ->where('slug = ?',$arrParam['slug']);
			$result=$this->fetchRow($select)->toArray();  
		}
		
		return $result;
	}
	
	public function deleteItem($arrParam=null,$options=null)
	{
		if($options['task'] == 'admin-delete'){
			$db = $this->getAdapter();
			$id = $arrParam['id'];
			$select = $db->select()
						 ->from('service_category AS c');
			$result  = $db->fetchAll($select);
			$recursive = new Zendme_System_Recursive($result);
			$result = $recursive->buildArray($arrParam['id']);
			$selectParent = $db->select()
						 ->from('service_category AS c',array('id','picture'))
						 ->where('id = ?',$arrParam['id']);
			$parent = $db->fetchRow($selectParent);
			array_unshift($result,array('id'=>$arrParam['id'],'picture'=>$parent['picture']));
			foreach ($result as $key=>$value){			
				$where = 'id = ' .$value['id'];
				$this->delete($where);
				$upload_dir = FILES_PATH . '/category';
				$upload = new Zendme_File_Upload();
				$upload->removeFile($upload_dir . '/orignal/' . $value['picture']);
				$upload->removeFile($upload_dir . '/images100x100/' . $value['picture']);
				$upload->removeFile($upload_dir . '/images450x450/' . $value['picture']);
			}			
		}		
		if($options['task'] == 'admin-multi-delete'){			
			$db = $this->getAdapter();
			$cid = $arrParam['cid'];
			$select = $db->select()
						 ->from('service_category AS c',array('id','name','picture','status','order','parents','created_by'));
			$result  = $db->fetchAll($select);
			$newArr = array();
			foreach ($cid as $key1 => $val1){				
				$id = $val1;
				$newArr[] = array('id'=>$id);
				$recursive = new Zendme_System_Recursive($result);
				$tmp = $recursive->buildArray($id);	
				if(count($tmp)>0){
					foreach ($tmp as $keyTmp => $valTmp){
						$newArr[] = $valTmp;							
					}
				}
			}
			$ids = '(';
			foreach ($newArr as $key => $value){				
				$ids .= $value['id'] . ',';
			}
			$ids .= '0)';
			$selectInfo = $db->select()
						 ->from('service_category AS c',array('id','picture'))
						 ->where('id IN' . $ids);
			$infoDelete = $db->fetchAll($selectInfo);
			echo $selectInfo;
			foreach ($infoDelete as $keyNew => $valNew){
				$id = $valNew['id'];
				$where = 'id = ' .$id;
				$this->delete($where);
				$upload_dir = FILES_PATH . '/category';
				$upload = new Zendme_File_Upload();
				$upload->removeFile($upload_dir . '/orignal/' . $valNew['picture']);
				$upload->removeFile($upload_dir . '/images100x100/' . $valNew['picture']);
				$upload->removeFile($upload_dir . '/images450x450/' . $valNew['picture']);
			}			
		}
	}
	
	public function changeStatus($arrParam=null,$options=null)
	{	
		$cid = $arrParam['cid'];		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}			
	}
	
	public function changesetHome($arrParam=null,$options=null)
	{
		$cid = $arrParam['cid'];		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$setHome = 1;
			}else{
				$setHome = 0;
			}			
			$data = array('home'=>$setHome);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}		
	}
	
	public function itemSelectBox($arrParam=null,$options=null)
	{
		$db=$this->getAdapter();
		if($options==null)
		{
			$select=$db->select()
					   ->from('service_category')
					   ->order('order ASC');
			$result=$db->fetchAll($select);					   
		}
		if($options['task']=='admin-edit')
		{
			$id=$arrParam['id'];
			$select=$db->select()
					   ->from('service_category')
					   ->order('order ASC')
					   ->where('id !=?',$id,INTEGER);
			$result=$db->fetchAll($select);		
		}
		
		$recursive= new Zendme_System_Recursive($result);
		$result=$recursive->buildArray(0);
		$tmp=array('id'=>0,'parents'=>0,'level'=>1,'name'=>'Thư mục gốc');
		array_unshift($result, $tmp);
		return $result;
		
	}
	
}